package cn.shop.merchant.service;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.merchant.mapper.user.MerchantRoleMapper;
import cn.shop.merchant.model.user.MerchantRole;
import cn.shop.merchant.req.AssignRoleReq;
import cn.shop.merchant.req.MerchantRoleQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-17 11:07
 * @description :
 */
@Service
public class MerchantRoleService extends BaseServiceImpl<MerchantRoleMapper, MerchantRole> {

	public boolean assignRole(AssignRoleReq req, long uid) {
		baseMapper.removeRoleByUserId(req.getUserId());
		req.setUid(uid);
		boolean res = SqlHelper.retBool(baseMapper.assignRole(req));
		if (res) {
			//删除该用户登录的角色权限
			SaSession session = StpUtil.getSessionByLoginId(req.getUserId(), false);
			if (null != session) {
				session.delete("Role_List");
			}
		}
		return res;
	}

	public List<String> permissionCodeByRoleCode(String code) {
		return baseMapper.permissionCodeByRoleCode(code);
	}

	public List<Long> queryPermissionsByRoleId(long id) {
		return baseMapper.queryPermissionsByRoleId(id);
	}

	public List<Map<String, Object>> allRole(long uid) {
		return list(Wrappers.<MerchantRole>lambdaQuery().eq(MerchantRole::getUid, uid).or(q -> q.eq(MerchantRole::getUid, 0))).stream()
				.map(r -> Putif.start()
						.anyway("label", r.getName())
						.anyway("value", r.getId())
						.get())
				.collect(toList());
	}

	public Map<String, Object> list(MerchantRoleQueryReq req, long uid) {
		Page<MerchantRole> page = Page.of(req.getCurrent(), req.getSize());
		LambdaQueryWrapper<MerchantRole> query = Wrappers.<MerchantRole>lambdaQuery()
				.eq(MerchantRole::getUid, uid)
				.eq(StrUtil.isNotBlank(req.getCode()), MerchantRole::getCode, req.getCode())
				.apply(notNull.test(req.getStartTime()), req.gtTime("create_time"), req.getStartTime())
				.apply(notNull.test(req.getEndTime()), req.ltTime("create_time"), req.getEndTime())
				.orderBy(notZero.test(req.getOrderByTime()), isAsc.test(req.getOrderByTime()), MerchantRole::getCreateTime);
		page = page(page, query);
		return PageUtil.formatPage(page, this::format);
	}

	public boolean hasCode(String code) {
		long count = count(Wrappers.<MerchantRole>lambdaQuery()
				.eq(MerchantRole::getCode, code));
		return count > 0;
	}

	public boolean update(MerchantRole role, long uid) {
		return update(role, Wrappers.<MerchantRole>lambdaUpdate()
				.eq(MerchantRole::getId, role.getId())
				.eq(MerchantRole::getUid, uid));
	}

	public boolean delete(long[] ids, long uid) {
		return SqlHelper.retBool(getBaseMapper().batchDeleteWithFill(new MerchantRole(),
				Wrappers.<MerchantRole>lambdaQuery()
						.in(MerchantRole::getId, Arrays.stream(ids).boxed().collect(toList()))
						.eq(MerchantRole::getUid, uid)));
	}

	public Map<String, Object> info(long id, long uid) {
		MerchantRole role = getOne(Wrappers.<MerchantRole>lambdaQuery()
				.eq(MerchantRole::getId, id)
				.eq(MerchantRole::getUid, uid));
		return format(role);
	}

	/**
	 * 获取用户关联的角色
	 *
	 * @param id 账户id
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:18
	 */
	public List<Long> getRoleByUserId(Long id) {
		return baseMapper.getRoleByUserId(id);

	}

	private Map<String, Object> format(MerchantRole role) {
		return Putif.start()
				.anyway("id", role.getId())
				.anyway("name", role.getName())
				.anyway("code", role.getCode())
				.anyway("description", role.getDescription())
				.get();
	}

}
